{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Support Vector Machines "
      ],
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Support Vector Machines(SVM) is use to solve both linear and non-linear models. SVM is used for classification or regression problems. In addition, is a method that transform the data and figures the data transformations by finding optimal boundary between the outputs. "
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import pandas as pd\n",
        "\n",
        "import warnings\n",
        "warnings.filterwarnings(\"ignore\")\n",
        "\n",
        "# yahoo finance is used to fetch data \n",
        "import yfinance as yf\n",
        "yf.pdr_override()"
      ],
      "outputs": [],
      "execution_count": 1,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:21.356Z",
          "iopub.execute_input": "2021-06-11T03:08:21.361Z",
          "iopub.status.idle": "2021-06-11T03:08:21.666Z",
          "shell.execute_reply": "2021-06-11T03:08:21.690Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# input\n",
        "symbol = 'AMD'\n",
        "start = '2018-01-01'\n",
        "end = '2018-02-01'\n",
        "\n",
        "# Read data \n",
        "dataset = yf.download(symbol,start,end)\n",
        "\n",
        "# View Columns\n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[*********************100%***********************]  1 of 1 completed\n"
          ]
        },
        {
          "output_type": "execute_result",
          "execution_count": 2,
          "data": {
            "text/plain": "             Open   High    Low  Close  Adj Close     Volume\nDate                                                        \n2018-01-02  10.42  11.02  10.34  10.98      10.98   44146300\n2018-01-03  11.61  12.14  11.36  11.55      11.55  154066700\n2018-01-04  12.10  12.43  11.97  12.12      12.12  109503000\n2018-01-05  12.19  12.22  11.66  11.88      11.88   63808900\n2018-01-08  12.01  12.30  11.85  12.28      12.28   63346000",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Open</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Close</th>\n      <th>Adj Close</th>\n      <th>Volume</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2018-01-02</th>\n      <td>10.42</td>\n      <td>11.02</td>\n      <td>10.34</td>\n      <td>10.98</td>\n      <td>10.98</td>\n      <td>44146300</td>\n    </tr>\n    <tr>\n      <th>2018-01-03</th>\n      <td>11.61</td>\n      <td>12.14</td>\n      <td>11.36</td>\n      <td>11.55</td>\n      <td>11.55</td>\n      <td>154066700</td>\n    </tr>\n    <tr>\n      <th>2018-01-04</th>\n      <td>12.10</td>\n      <td>12.43</td>\n      <td>11.97</td>\n      <td>12.12</td>\n      <td>12.12</td>\n      <td>109503000</td>\n    </tr>\n    <tr>\n      <th>2018-01-05</th>\n      <td>12.19</td>\n      <td>12.22</td>\n      <td>11.66</td>\n      <td>11.88</td>\n      <td>11.88</td>\n      <td>63808900</td>\n    </tr>\n    <tr>\n      <th>2018-01-08</th>\n      <td>12.01</td>\n      <td>12.30</td>\n      <td>11.85</td>\n      <td>12.28</td>\n      <td>12.28</td>\n      <td>63346000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 2,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:21.675Z",
          "iopub.execute_input": "2021-06-11T03:08:21.681Z",
          "iopub.status.idle": "2021-06-11T03:08:22.276Z",
          "shell.execute_reply": "2021-06-11T03:08:22.504Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset['Increase_Decrease'] = np.where(dataset['Volume'].shift(-1) > dataset['Volume'],1,0)\n",
        "dataset['Buy_Sell_on_Open'] = np.where(dataset['Open'].shift(-1) > dataset['Open'],1,0)\n",
        "dataset['Buy_Sell'] = np.where(dataset['Adj Close'].shift(-1) > dataset['Adj Close'],1,0)\n",
        "dataset['Returns'] = dataset['Adj Close'].pct_change()\n",
        "dataset = dataset.dropna()\n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 3,
          "data": {
            "text/plain": "             Open   High    Low  Close  Adj Close     Volume  \\\nDate                                                           \n2018-01-03  11.61  12.14  11.36  11.55      11.55  154066700   \n2018-01-04  12.10  12.43  11.97  12.12      12.12  109503000   \n2018-01-05  12.19  12.22  11.66  11.88      11.88   63808900   \n2018-01-08  12.01  12.30  11.85  12.28      12.28   63346000   \n2018-01-09  12.00  12.02  11.71  11.82      11.82   62560900   \n\n            Increase_Decrease  Buy_Sell_on_Open  Buy_Sell   Returns  \nDate                                                                 \n2018-01-03                  0                 1         1  0.051913  \n2018-01-04                  0                 1         0  0.049351  \n2018-01-05                  0                 0         1 -0.019802  \n2018-01-08                  0                 0         0  0.033670  \n2018-01-09                  0                 0         1 -0.037459  ",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Open</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Close</th>\n      <th>Adj Close</th>\n      <th>Volume</th>\n      <th>Increase_Decrease</th>\n      <th>Buy_Sell_on_Open</th>\n      <th>Buy_Sell</th>\n      <th>Returns</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2018-01-03</th>\n      <td>11.61</td>\n      <td>12.14</td>\n      <td>11.36</td>\n      <td>11.55</td>\n      <td>11.55</td>\n      <td>154066700</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.051913</td>\n    </tr>\n    <tr>\n      <th>2018-01-04</th>\n      <td>12.10</td>\n      <td>12.43</td>\n      <td>11.97</td>\n      <td>12.12</td>\n      <td>12.12</td>\n      <td>109503000</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0.049351</td>\n    </tr>\n    <tr>\n      <th>2018-01-05</th>\n      <td>12.19</td>\n      <td>12.22</td>\n      <td>11.66</td>\n      <td>11.88</td>\n      <td>11.88</td>\n      <td>63808900</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>-0.019802</td>\n    </tr>\n    <tr>\n      <th>2018-01-08</th>\n      <td>12.01</td>\n      <td>12.30</td>\n      <td>11.85</td>\n      <td>12.28</td>\n      <td>12.28</td>\n      <td>63346000</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.033670</td>\n    </tr>\n    <tr>\n      <th>2018-01-09</th>\n      <td>12.00</td>\n      <td>12.02</td>\n      <td>11.71</td>\n      <td>11.82</td>\n      <td>11.82</td>\n      <td>62560900</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>-0.037459</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 3,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.287Z",
          "iopub.execute_input": "2021-06-11T03:08:22.293Z",
          "iopub.status.idle": "2021-06-11T03:08:22.308Z",
          "shell.execute_reply": "2021-06-11T03:08:22.509Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset['Target'] = 0\n",
        "dataset.loc[dataset['Returns']>0, 'Target'] = 1\n",
        "dataset.loc[dataset['Returns']<0, 'Target'] = -1"
      ],
      "outputs": [],
      "execution_count": 4,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.318Z",
          "iopub.execute_input": "2021-06-11T03:08:22.324Z",
          "iopub.status.idle": "2021-06-11T03:08:22.334Z",
          "shell.execute_reply": "2021-06-11T03:08:22.513Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 5,
          "data": {
            "text/plain": "             Open   High    Low  Close  Adj Close     Volume  \\\nDate                                                           \n2018-01-03  11.61  12.14  11.36  11.55      11.55  154066700   \n2018-01-04  12.10  12.43  11.97  12.12      12.12  109503000   \n2018-01-05  12.19  12.22  11.66  11.88      11.88   63808900   \n2018-01-08  12.01  12.30  11.85  12.28      12.28   63346000   \n2018-01-09  12.00  12.02  11.71  11.82      11.82   62560900   \n\n            Increase_Decrease  Buy_Sell_on_Open  Buy_Sell   Returns  Target  \nDate                                                                         \n2018-01-03                  0                 1         1  0.051913       1  \n2018-01-04                  0                 1         0  0.049351       1  \n2018-01-05                  0                 0         1 -0.019802      -1  \n2018-01-08                  0                 0         0  0.033670       1  \n2018-01-09                  0                 0         1 -0.037459      -1  ",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Open</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Close</th>\n      <th>Adj Close</th>\n      <th>Volume</th>\n      <th>Increase_Decrease</th>\n      <th>Buy_Sell_on_Open</th>\n      <th>Buy_Sell</th>\n      <th>Returns</th>\n      <th>Target</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2018-01-03</th>\n      <td>11.61</td>\n      <td>12.14</td>\n      <td>11.36</td>\n      <td>11.55</td>\n      <td>11.55</td>\n      <td>154066700</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.051913</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2018-01-04</th>\n      <td>12.10</td>\n      <td>12.43</td>\n      <td>11.97</td>\n      <td>12.12</td>\n      <td>12.12</td>\n      <td>109503000</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0.049351</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2018-01-05</th>\n      <td>12.19</td>\n      <td>12.22</td>\n      <td>11.66</td>\n      <td>11.88</td>\n      <td>11.88</td>\n      <td>63808900</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>-0.019802</td>\n      <td>-1</td>\n    </tr>\n    <tr>\n      <th>2018-01-08</th>\n      <td>12.01</td>\n      <td>12.30</td>\n      <td>11.85</td>\n      <td>12.28</td>\n      <td>12.28</td>\n      <td>63346000</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.033670</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2018-01-09</th>\n      <td>12.00</td>\n      <td>12.02</td>\n      <td>11.71</td>\n      <td>11.82</td>\n      <td>11.82</td>\n      <td>62560900</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>-0.037459</td>\n      <td>-1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 5,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.345Z",
          "iopub.execute_input": "2021-06-11T03:08:22.351Z",
          "iopub.status.idle": "2021-06-11T03:08:22.367Z",
          "shell.execute_reply": "2021-06-11T03:08:22.517Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#split dataset in features and target variable\n",
        "feature_cols = ['Adj Close']\n",
        "X = dataset[feature_cols] # Features\n",
        "Y = dataset.Target # Target variable"
      ],
      "outputs": [],
      "execution_count": 6,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.379Z",
          "iopub.execute_input": "2021-06-11T03:08:22.386Z",
          "iopub.status.idle": "2021-06-11T03:08:22.397Z",
          "shell.execute_reply": "2021-06-11T03:08:22.520Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "print(X.shape)\n",
        "print(Y.shape)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(20, 1)\n",
            "(20,)\n"
          ]
        }
      ],
      "execution_count": 7,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.407Z",
          "iopub.execute_input": "2021-06-11T03:08:22.415Z",
          "iopub.status.idle": "2021-06-11T03:08:22.430Z",
          "shell.execute_reply": "2021-06-11T03:08:22.524Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X = np.array(X).reshape(20,-1)\n",
        "Y = np.array(Y).reshape(20,-1)"
      ],
      "outputs": [],
      "execution_count": 8,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.442Z",
          "iopub.execute_input": "2021-06-11T03:08:22.449Z",
          "iopub.status.idle": "2021-06-11T03:08:22.458Z",
          "shell.execute_reply": "2021-06-11T03:08:22.528Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# split X and y into training and testing sets\n",
        "from sklearn.model_selection import train_test_split\n",
        "X_train,X_test,y_train,y_test=train_test_split(X,Y,test_size=0.20)"
      ],
      "outputs": [],
      "execution_count": 9,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.468Z",
          "iopub.execute_input": "2021-06-11T03:08:22.476Z",
          "iopub.status.idle": "2021-06-11T03:08:22.701Z",
          "shell.execute_reply": "2021-06-11T03:08:22.693Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "print(X_train.shape)\n",
        "print(y_train.shape)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(16, 1)\n",
            "(16, 1)\n"
          ]
        }
      ],
      "execution_count": 10,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.712Z",
          "iopub.execute_input": "2021-06-11T03:08:22.718Z",
          "iopub.status.idle": "2021-06-11T03:08:22.732Z",
          "shell.execute_reply": "2021-06-11T03:08:22.916Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn import svm\n",
        "\n",
        "# instantiate the model (using the default parameters)\n",
        "clf = svm.SVC(kernel='linear', gamma=1)\n",
        "\n",
        "# fit the model with data\n",
        "clf.fit(X, Y)\n",
        "\n",
        "y_pred = clf.predict(Y)"
      ],
      "outputs": [],
      "execution_count": 11,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.742Z",
          "iopub.execute_input": "2021-06-11T03:08:22.748Z",
          "iopub.status.idle": "2021-06-11T03:08:22.759Z",
          "shell.execute_reply": "2021-06-11T03:08:22.921Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "clf.score(X, Y)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 12,
          "data": {
            "text/plain": "0.65"
          },
          "metadata": {}
        }
      ],
      "execution_count": 12,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.767Z",
          "iopub.execute_input": "2021-06-11T03:08:22.772Z",
          "iopub.status.idle": "2021-06-11T03:08:22.784Z",
          "shell.execute_reply": "2021-06-11T03:08:22.925Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#Predict Output\n",
        "y_pred[0]"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 13,
          "data": {
            "text/plain": "1"
          },
          "metadata": {}
        }
      ],
      "execution_count": 13,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.794Z",
          "iopub.execute_input": "2021-06-11T03:08:22.801Z",
          "iopub.status.idle": "2021-06-11T03:08:22.813Z",
          "shell.execute_reply": "2021-06-11T03:08:22.928Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# date = np.reshape(dataset.index,(len(dataset.index),1))\n",
        "date = np.array(dataset.index).reshape(20,-1)\n",
        "price = np.array(dataset['Adj Close']).reshape(20,-1)"
      ],
      "outputs": [],
      "execution_count": 14,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.825Z",
          "iopub.execute_input": "2021-06-11T03:08:22.831Z",
          "iopub.status.idle": "2021-06-11T03:08:22.839Z",
          "shell.execute_reply": "2021-06-11T03:08:22.931Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Clheck for nan\n",
        "np.isnan(price)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 15,
          "data": {
            "text/plain": "array([[False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False],\n       [False]])"
          },
          "metadata": {}
        }
      ],
      "execution_count": 15,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.846Z",
          "iopub.execute_input": "2021-06-11T03:08:22.852Z",
          "iopub.status.idle": "2021-06-11T03:08:22.863Z",
          "shell.execute_reply": "2021-06-11T03:08:22.934Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# date = date[~np.isnan(date)]\n",
        "price = price[~np.isnan(price)]"
      ],
      "outputs": [],
      "execution_count": 16,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.872Z",
          "iopub.execute_input": "2021-06-11T03:08:22.879Z",
          "iopub.status.idle": "2021-06-11T03:08:22.887Z",
          "shell.execute_reply": "2021-06-11T03:08:22.937Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.svm import SVR\n",
        "\n",
        "svr_lin = SVR(kernel='linear',C=1e3)\n",
        "svr_poly = SVR(kernel='poly',C=1e3,degree=1)\n",
        "svr_rbf = SVR(kernel='rbf',C=1e3,gamma=0.1)\n",
        "svr_sig = SVR(kernel='sigmoid', gamma=2)\n",
        "\n",
        "svr_lin.fit(date,price)\n",
        "svr_poly.fit(date,price)\n",
        "svr_rbf.fit(date,price)\n",
        "svr_sig.fit(date,price)"
      ],
      "outputs": [],
      "execution_count": 17,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-06-11T03:08:22.896Z",
          "iopub.execute_input": "2021-06-11T03:08:22.901Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "plt.scatter(date,price,color='black',label='data')\n",
        "plt.plot(date,svr_lin.predict(date),color='blue',label='Linear SVR')\n",
        "plt.plot(date,svr_poly.predict(date),color='red',label='Polynomial SVR')\n",
        "plt.plot(date,svr_rbf.predict(date),color='green',label='RBF SVR')\n",
        "plt.plot(date,svr_sig.predict(date),color='orange',label='Sigmoid SVR')\n",
        "plt.xlabel('Dates')\n",
        "plt.ylabel('Price')\n",
        "plt.title('Support Vector Regression')\n",
        "plt.legend()\n",
        "plt.show()"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "print('Linear SVR Score:', svr_lin.score(date,price))\n",
        "print('Sigmoid SVR Score:', svr_sig.score(date,price))\n",
        "print('RBF SVR Score:', svr_rbf.score(date,price))\n",
        "print('Polynomial SVR Score:', svr_poly.score(date,price))"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "print(svr_lin.predict(10)[0])\n",
        "print(svr_poly.predict(10)[0])\n",
        "print(svr_rbf.predict(10)[0])\n",
        "print(svr_sig.predict(10)[0])"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.svm import SVC\n",
        "\n",
        "X = np.array(dataset[['Open', 'High']])\n",
        "y = np.array(dataset['Buy_Sell'])\n",
        "clf = SVC(gamma='auto')\n",
        "clf.fit(X, Y) "
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def plotSVC(title):\n",
        "    # create a mesh to plot in\n",
        "    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1\n",
        "    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1\n",
        "    h = (x_max / x_min)/100\n",
        "    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),\n",
        "    np.arange(y_min, y_max, h))\n",
        "\n",
        "    plt.subplot(1, 1, 1)\n",
        "    Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])\n",
        "    Z = Z.reshape(xx.shape)\n",
        "    plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)\n",
        "\n",
        "    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)\n",
        "    plt.xlabel('Date')\n",
        "    plt.ylabel('Price')\n",
        "    plt.xlim(xx.min(), xx.max())\n",
        "    plt.title(title)\n",
        "    plt.show()"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "kernels = ['linear', 'rbf', 'poly']\n",
        "\n",
        "for kernel in kernels:\n",
        "  svc = svm.SVC(kernel=kernel).fit(X, y)\n",
        "  plotSVC('kernel=' + str(kernel))"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "gammas = [0.1, 1, 10, 100]\n",
        "\n",
        "for gamma in gammas:\n",
        "   svc = svm.SVC(kernel='rbf', gamma=gamma).fit(X, y)\n",
        "   plotSVC('gamma=' + str(gamma))"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "cs = [0.1, 1, 10, 100, 1000]\n",
        "\n",
        "for c in cs:\n",
        "   svc = svm.SVC(kernel='rbf', C=c).fit(X, y)\n",
        "   plotSVC('C=' + str(c))"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "degrees = [0, 1, 2, 3, 4, 5, 6]\n",
        "\n",
        "for degree in degrees:\n",
        "   svc = svm.SVC(kernel='poly', degree=degree).fit(X, y)\n",
        "   plotSVC('degree=' + str(degree))"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.6.12",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    },
    "kernelspec": {
      "name": "python3",
      "language": "python",
      "display_name": "Python 3"
    },
    "nteract": {
      "version": "0.28.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}